const {merge} = require('webpack-merge');
const os = require('os')
const threads = os.cpus().length;
const path = require("path");
const {VueLoaderPlugin} = require('vue-loader');
const TerserPlugin = require('terser-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const {DefinePlugin, ProvidePlugin} = require("webpack");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const StylelintWebpackPlugin = require('stylelint-webpack-plugin');
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const webpackBaseConfig = require('./config/webpack.base.config');
const webpackDevConfig = require('./config/webpack.dev.config');
const webpackProdConfig = require('./config/webpack.prod.config');
module.exports = (env) => {
    const webpackVariable = {
        mode: env['WEBPACK_SERVE'] ? 'development' : 'production',
        runEnv: env['runEnv']
    }
    const rootPath = path.resolve(__dirname, './')
    const devConfig = webpackDevConfig({ESLintPlugin, StylelintWebpackPlugin})
    const prodConfig = webpackProdConfig({
        TerserPlugin,
        CompressionPlugin,
        CssMinimizerPlugin,
        MiniCssExtractPlugin,
        ImageMinimizerPlugin,
        BundleAnalyzerPlugin
    })
    const baseConfig = webpackBaseConfig({
        threads,
        rootPath,
        DefinePlugin,
        ProvidePlugin,
        VueLoaderPlugin,
        webpackVariable,
        HtmlWebpackPlugin,
        CopyWebpackPlugin,
        MiniCssExtractPlugin,
        ForkTsCheckerWebpackPlugin
    })
    switch (webpackVariable.mode) {
        case 'development':
            return merge(devConfig, baseConfig);
        case 'production':
            return merge(prodConfig, baseConfig);
        default:
            throw new Error('No matching configuration was found!');
    }
}
// https://www.cnblogs.com/gluncle/p/8608723.html
